package TUILayer;
import java.util.Scanner;
import ControlLayer.LoanCtr;
/**
 * User interface to the Loan controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: loanmenu changed to If                   
 */

public class LoanUI
{
    private LoanCtr loanCtr;//Loan UI must know the Loan controller 

    public LoanUI()
    {
        loanCtr = new LoanCtr();
    }

    public void start()
    {
        loanMenu();
    }

    public void loanMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice = writeLoanMenu();
            switch(choice){
                case 1:
                findLoan();
                break;
                case 2:
                createLoan();
                break;
                case 3:
                deleteLoan();
                break;
                case 4:
                updateLoan();
                break;
                case 5:
                listAllLoans();
                break;
                case 6:
                returnLoan();
                break;
                default:
                exit = true;
                break;
            }
        }
    }

    private int writeLoanMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Loan Menu ***");
        System.out.println(" (1) Show Loan information");
        System.out.println(" (2) Create Loan description");
        System.out.println(" (3) Delete Loan");
        System.out.println(" (4) Update Loan");       
        System.out.println(" (5) Show all Loans");
        System.out.println(" (6) Return Loan");
        System.out.println(" (7) Return");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    private int inputLoanno()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Loan id: " );
        int out = keyboard.nextInt();
        return out;
    }

    private int inputDVDno()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD id: " );
        int out = keyboard.nextInt();
        return out;
    }

    private int inputCopyno()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Copy serial number: " );
        int out = keyboard.nextInt();
        return out;
    }

    private long inputPersonno()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the person id: " );
        long out = keyboard.nextLong();
        return out;
    }

    private String inputLoandate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the loan date: " );
        String out = keyboard.nextLine();
        return out;
    }

    private String inputReturndate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the return date: " );
        String out = keyboard.nextLine();
        return out;
    }

    private boolean inputActive()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type if active or not (y/n): " );
        String answer = keyboard.nextLine();
        if(answer == "y")
            return true;
        else 
            return false;
    }

    //there is need for more methods to read information about Loans
    //and methods to display the information

    private void findLoan()
    {     
        Scanner keyboard = new Scanner(System.in);
        int no = inputLoanno();
        if(loanCtr.findLoan(no) != null)
        {
            loanCtr.findLoan(no).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("Couldn't find any loan with the id "+no+" - try another id");
            System.out.println();
            System.out.println("Enter to continue...");
        }
        keyboard.nextLine();
    }

    private void createLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        
        int idDVD = inputDVDno();
        int idCopy = inputCopyno();
        long idPers = inputPersonno();
        String loanDate = inputLoandate();

        if(loanCtr.getSizeOfDVD() != 0 && loanCtr.getSizeOfPers() != 0)
        {
            if(loanCtr.findDVD(idDVD) != null && loanCtr.findDVD(idDVD).findCopy(idCopy) != null && loanCtr.findPerson(idPers) != null)
            {
                loanCtr.createLoan(idDVD, idCopy, idPers, loanDate);
            }
            else
            {
                System.out.println("Ops! this is a very tricky system, you have to enter the ids exactly the way they are");
                System.out.println("try again - And good luck ;) ");
            }
        }
        else
        {
            System.out.println("Couldn't find any DVDs or any Persons - please try again");
            System.out.println();
            System.out.println("Create a Person in *Addressbook* menu"); 
            System.out.print("and"); 
            System.out.println("create a DVD in *DVDs* menu");
        }
        
        int id = loanCtr.getID(); 
        if(loanCtr.findLoan(id) != null)
        {
            System.out.println();
            System.out.println("New Loan "+ id+" is created - see info above");
            loanCtr.findLoan(id).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");            
        }
        else
        {
            System.out.println();
            System.out.println("Something went wrong - contact an administrator or try again! ");
            System.out.println();
            System.out.println("Enter to continue...");
        }

        keyboard.nextLine();

    }

    private void updateLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        int id = inputLoanno();
        if(loanCtr.findLoan(id) != null)
        {
            loanCtr.updateLoan(id, inputDVDno(), inputCopyno(), inputPersonno(), inputLoandate(), inputReturndate(), inputActive());
            System.out.println();
            System.out.println("The Loan with the id: "+id+" is now updated");
            System.out.println();
            loanCtr.findLoan(id).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("No Loans is updated - id "+id+" dosen't exist !");
            System.out.println();
            System.out.println("Enter to continue...");

        }
        keyboard.nextLine();

    }

    private void deleteLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        int id = inputLoanno();

        if(loanCtr.findLoan(id) != null)
        {
            System.out.println();
            System.out.println("The Loan with the id: "+id+" is now deleted");            
            loanCtr.findLoan(id).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");
            keyboard.nextLine();
            loanCtr.deleteLoan(id);
        }
        else
        {
            System.out.println();
            System.out.println("Couldn't find any loan with the id "+id+" - try another id");
            System.out.println();
            System.out.println("Enter to continue..."); 
            keyboard.nextLine();
        }

    }

    private void returnLoan()
    {
        Scanner keyboard = new Scanner(System.in);

        int id = inputLoanno();

        if(loanCtr.findLoan(id) != null)
        {
            loanCtr.returnLoan(id, inputReturndate());
            System.out.println();
            System.out.println("The Loan with the id: "+id+" is now deleted");            
            loanCtr.findLoan(id).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");           
        }
        else
        {
            System.out.println();
            System.out.println("Couldn't find any loan with the id "+id+" - try another id");
            System.out.println();
            System.out.println("Enter to continue..."); 

        }
        keyboard.nextLine();

    }

    private void listAllLoans()
    {
        Scanner keyboard = new Scanner(System.in);
        if(loanCtr.getSizeLoan() != 0)
        {
            
            loanCtr.listAllLoans();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("Couldn't find any loan");
            System.out.println();
            System.out.println("Enter to continue..."); 
        }

        keyboard.nextLine();
    }
}